package personal.wang.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.UnhandledErrorListener;
import org.apache.curator.framework.recipes.leader.LeaderLatch;
import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import personal.wang.config.DataMockYamlReader;
import personal.wang.constant.ConstantVar;
import personal.wang.utils.YamlReader;

/**
 * 源码中的 ZooKeeperLeaderRetrievalDriver
 */
public class HAUtils {
    private static final Logger logger = LoggerFactory.getLogger(HAUtils.class);
    public static CuratorFramework buildClient(){
        String zkQuorum = DataMockYamlReader.getValueAsString(ConstantVar.ZOOKEEPER_CONNECTION);
        int sessionTimeout = 60000;
        int connectionTimeout = 60000;
        int retryWait = 100;
        int maxRetryAttempts = 3;
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
                .connectString(zkQuorum)
                .sessionTimeoutMs(sessionTimeout)
                .connectionTimeoutMs(connectionTimeout)
                .retryPolicy(new ExponentialBackoffRetry(retryWait, maxRetryAttempts));
        CuratorFramework cf = builder.build();
        return cf ;
    }
}
